package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryTreeItemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/categories")
@Api(tags = "01.类别管理模块")
@Slf4j
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)

    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam) {
        log.debug("开始处理[添加类别]的请求，参数{}", categoryAddNewParam);
        categoryService.addNew(categoryAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据Id删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")

    })
    //检查未封装的参数       ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    public JsonResult delete(@Range(min = 1, message = "根据ID删除类别失败，请提交合法的Id值！")
                                 @PathVariable Long id) {
        log.debug("开始处理【根据ID删除类别】的请求，参数：{}", id);
        categoryService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID更新类别")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "Long")
    public JsonResult updateById(
            @Range(min = 1, message = "根据ID更新类别失败，请提交合法的ID值！")
            @PathVariable Long id,
            @Valid CategoryUpdateInfoParam categoryUpdateInfoParam){
        categoryService.updateInfoById(id, categoryUpdateInfoParam);

        return JsonResult.ok();
    }


    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询类别失败，请提交合法的ID值！")
            @PathVariable Long id){
        CategoryStandardVO standardById = categoryService.getStandardById(id);
        return JsonResult.ok(standardById);
    }


    @GetMapping("/list-by-parent")
    @ApiOperation("查询指定父级下的列表")
    @ApiOperationSupport(order = 415)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult listByParentId(Long parentId,
                                     @Range(min = 1, message = "查询类别列表失败，请提供正确的页码值!") Integer pageNum,
                                     String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<CategoryListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = categoryService.listByParentId(parentId, pageNum, Integer.MAX_VALUE);
        }else{
            pageData = categoryService.listByParentId(parentId, pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable
                                 @Range(min =1 , message = "请提交有效的ID值！")Long id ){
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable
                                @Range(min =1 , message = "请提交有效的ID值！")Long id ){
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/display")
    @ApiOperation("显示在导航栏")
    @ApiOperationSupport(order = 312)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisplay(@PathVariable
                                 @Range(min =1 , message = "请提交有效的ID值！")Long id ){
        categoryService.setDisplay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/hidden")
    @ApiOperation("禁用在导航栏")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setHidden(@PathVariable
                                 @Range(min =1 , message = "请提交有效的ID值！")Long id ){
        categoryService.setHidden(id);
        return JsonResult.ok();
    }

    @GetMapping("/tree")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 420)
    public JsonResult listTree() {
        List<CategoryTreeItemVO> categoryTree = categoryService.listTree();
        return JsonResult.ok(categoryTree);
    }



}
